package com.hongyun.tms.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import com.hongyun.tms.common.PageResponseDto;
import com.hongyun.tms.common.constants.DefaultCreateByConstant;
import com.hongyun.tms.common.constants.DeleteReferenceConstant;
import com.hongyun.tms.common.dto.mship.MShipQueryDto;
import com.hongyun.tms.common.dto.mship.MShipSaveReqDto;
import com.hongyun.tms.common.dto.tug.SelecSearchDTO;
import com.hongyun.tms.common.utils.MapperingUtils;
import com.hongyun.tms.common.utils.ReferenceCheckUtil;
import com.hongyun.tms.common.vo.MShipVO;
import com.hongyun.tms.entity.MShip;
import com.hongyun.tms.entity.PordWork;
import com.hongyun.tms.entity.ProdPlan;
import com.hongyun.tms.mapper.MShipMapper;
import com.hongyun.tms.service.IMShipService;
import com.hongyun.tms.service.IPordWorkService;
import com.hongyun.tms.service.IProdPlanService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 大船规范 服务实现类
 * </p>
 *
 * @author liPeng
 * @since 2024-06-11
 */
@Service
public class MShipServiceImpl extends ServiceImpl<MShipMapper, MShip> implements IMShipService {

    @Resource
    @Lazy
    private IProdPlanService planService;
    @Resource
    private IPordWorkService workService;

    @Override
    public PageResponseDto<MShipVO> pageList(MShipQueryDto query) {
        Page<MShip> page = new Page<>(query.getStartPage(), query.getPageSize());
        baseMapper.selectPage(page, new LambdaQueryWrapper<MShip>()
                .eq(StringUtils.isNotBlank(query.getShipKindCode()), MShip::getShipKindCode, query.getShipKindCode())
                .eq(StringUtils.isNotBlank(query.getNotionCode()), MShip::getNotionCode, query.getNotionCode())
                .like(StringUtils.isNotBlank(query.getShipName()), MShip::getShipName, query.getShipName())
        );
        return (PageResponseDto<MShipVO>) new PageResponseDto(page, MShipVO.class);
    }

    @Override
    public List<Map<String, Object>> getShipList(SelecSearchDTO selectCommonSearch) {
        return baseMapper.getShipList(selectCommonSearch);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteBatch(List<String> ids) {
        referenceCheck(ids);
        removeByIds(ids);
    }
    private void referenceCheck(List<String> shipIdList) {
        //作业计划引用check
        ReferenceCheckUtil.checkReference(planService, ProdPlan::getShipId, shipIdList, DeleteReferenceConstant.PROD_PLAN_MENU, MShip::getShipName, id -> getOne(new LambdaQueryWrapper<MShip>().eq(MShip::getId, id)));
        //作业记录引用check
        ReferenceCheckUtil.checkReference(workService, PordWork::getShipId, shipIdList, DeleteReferenceConstant.PORD_WORK_MENU, MShip::getShipName, id -> getOne(new LambdaQueryWrapper<MShip>().eq(MShip::getId, id)));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveOrUpdateShip(MShipSaveReqDto dto) {
        MShip ship = MapperingUtils.copyProperties(dto, MShip.class)
        .setUpdateTime(LocalDateTime.now())
        .setUpdateBy(DefaultCreateByConstant.userId)
        .setUpdateByName(DefaultCreateByConstant.userId.toString());
        if (dto.getId() == null) {
            ship.setCreateBy(DefaultCreateByConstant.userId);
            ship.setCreateByName(DefaultCreateByConstant.userId.toString());
            ship.setCreateTime(LocalDateTime.now());
            save(ship);
        } else {
            updateById(ship);
        }
    }

    @Override
    public List<MShipVO> export(MShipQueryDto query) {
        long currentPage = 1;
        List<MShipVO> allRecords = Lists.newArrayList();

        while (true) {
            query.setStartPage(currentPage);
            PageResponseDto<MShipVO> page = this.pageList(query);
            List<MShipVO> records = page.getRecords();
            allRecords.addAll(records);

            if (records.size() < query.getPageSize()) {
                // 当前页的记录数小于 pageSize，说明已经是最后一页
                break;
            }
            currentPage++;
        }

        return allRecords;
    }
}
